package com.whjk.safework.utils;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.mybatisflex.core.row.Row;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;
import java.util.List;

import static cn.hutool.core.date.DateUtil.offset;

public class LoaclDateUtil {

    public static DateTime offsetYear(Date date, int offset){
        return offset(date, DateField.YEAR, offset);
    }

    //查询下周的开始时间 从几天开始 num  开始天数。
    public static Row getNextWeekStartDay(int num){

        String  todaystr        =   DateUtil.today();//获取当天日期
        Date    todaydate       =   DateUtil.parse(todaystr+" 00:00:01", "yyyy-MM-dd HH:mm:ss");
        Date    newWeekday      =   DateUtil.offsetWeek(todaydate,1);
        int     daynum          =   DateUtil.thisDayOfWeek();
        int     offset          =   6-num;//从周日算一个周的第一天
        Date    startDay        =   DateUtil.offsetDay(newWeekday,-offset);
        Date    endDay          =   DateUtil.offsetDay(newWeekday,-offset+7);

        Row     row = new Row();
                row.set("startDay",startDay);
                row.set("endDay",endDay);
        return row;
    }


    public static Row getThisWeekStartDay(int num){

        String  todaystr        =   DateUtil.today();//获取当天日期
        System.out.println("todaystr:"+todaystr);
        Date    todaydate       =   DateUtil.parse(todaystr+" 00:00:01", "yyyy-MM-dd HH:mm:ss");
        int     daynum          =    DateUtil.dayOfWeek(todaydate); //DateUtil.thisDayOfWeek();

        System.out.println("index:"+daynum+"天");

        if(daynum==1){
            Date    startDay        =   DateUtil.offsetDay(todaydate,-6);
            Date    endDay          =   DateUtil.offsetDay(todaydate,1);
            Row     row = new Row();
            row.set("startDay",startDay);
            row.set("endDay",endDay);
            return row;
        }
        else {
            int     offset          =   daynum-2;//从周日算一个周的第一天
            System.out.println("offset:"+offset);
            Date    startDay        =   DateUtil.offsetDay(todaydate,-(offset));
            Date    endDay          =   DateUtil.offsetDay(startDay,7);
            Row     row = new Row();
            row.set("startDay",startDay);
            row.set("endDay",endDay);
            return row;
        }
    }

    public static Row getThisWeekStartDay(String  todaystr,int num){


        Date    todaydate       =   DateUtil.parse(todaystr+" 00:00:01", "yyyy-MM-dd HH:mm:ss");
        int     daynum          =    DateUtil.dayOfWeek(todaydate); //DateUtil.thisDayOfWeek();

       // System.out.println("daynum:"+daynum);

        if(daynum==1){
            Date    startDay        =   DateUtil.offsetDay(todaydate,-6);
            Date    endDay          =   DateUtil.offsetDay(todaydate,1);
            Row     row = new Row();
            row.set("startDay",startDay);
            row.set("endDay",endDay);
            return row;
        }
        else {
            int     offset          =   daynum-2;//从周日算一个周的第一天
            System.out.println("offset:"+offset);
            Date    startDay        =   DateUtil.offsetDay(todaydate,-(offset));
            Date    endDay          =   DateUtil.offsetDay(startDay,7);
            Row     row = new Row();
            row.set("startDay",startDay);
            row.set("endDay",endDay);
            return row;
        }
    }

    public static Row getThisWeekStartDay(int num,String todaystr){

        Date    todaydate       =   DateUtil.parse(todaystr+" 00:00:01", "yyyy-MM-dd HH:mm:ss");
        System.out.println("设置日期:"+todaydate);
        Date    newWeekday      =   DateUtil.offsetWeek(todaydate,1);
        // System.out.println("下个周:"+newWeekday);
        int     daynum          =    DateUtil.dayOfWeek(todaydate); //DateUtil.thisDayOfWeek();
        //  System.out.println("设置日期是本周第几天:"+daynum);
        if(daynum==1){
            Date    startDay        =   DateUtil.offsetDay(todaydate,-6);
            Date    endDay          =   todaydate;
            Row     row = new Row();
            row.set("startDay",startDay);
            row.set("endDay",endDay);
            return row;
        }
        else {
            int     offset          =   daynum-2;//从周日算一个周的第一天
            System.out.println("offset:"+offset);
            Date    startDay        =   DateUtil.offsetDay(todaydate,-(offset));
            Date    endDay          =   DateUtil.offsetDay(startDay,7);
            Row     row = new Row();
            row.set("startDay",startDay);
            row.set("endDay",endDay);
            return row;
        }
    }

    public static Row getNextMonthStartDay(int num){
        String  todaystr        =   DateUtil.today();//获取当天日期
        Date    todaydate       =   DateUtil.parse(todaystr+" 00:00:01", "yyyy-MM-dd HH:mm:ss");
        Date    enddate         =   DateUtil.parse(DateUtil.endOfMonth(todaydate).toDateStr()+" 00:00:01", "yyyy-MM-dd HH:mm:ss");
        Date    newMonthday     =   DateUtil.offsetDay(enddate,num-1);
        Date    endMonthday     =   DateUtil.offsetMonth(newMonthday,1);

        Row     row = new Row();
                row.set("startDay",newMonthday);
                row.set("endDay",endMonthday);
        return row;
    }

    public static Row getThisMonthStartDay(int num){

        String  todaystr        =   DateUtil.today();//获取当天日期
        Date    todaydate       =   DateUtil.parse(todaystr+" 00:00:01", "yyyy-MM-dd HH:mm:ss");
        Date    enddate         =   DateUtil.parse(DateUtil.beginOfMonth(todaydate).toDateStr()+" 00:00:01", "yyyy-MM-dd HH:mm:ss");
        Date    newMonthday     =   DateUtil.offsetDay(enddate,num-1);
        Date    endMonthday     =   DateUtil.offsetMonth(newMonthday,1);

        Row     row = new Row();
        row.set("startDay",newMonthday);
        row.set("endDay",endMonthday);
        return row;
    }

    public static Row getThisMonthStartDay(int num,String todaystr){
        Date    todaydate       =   DateUtil.parse(todaystr+" 00:00:01", "yyyy-MM-dd HH:mm:ss");
        Date    enddate         =   DateUtil.parse(DateUtil.beginOfMonth(todaydate).toDateStr()+" 00:00:01", "yyyy-MM-dd HH:mm:ss");
        Date    newMonthday     =   DateUtil.offsetDay(enddate,num-1);
        Date    endMonthday     =   DateUtil.offsetMonth(newMonthday,1);

        Row     row = new Row();
        row.set("startDay",newMonthday);
        row.set("endDay",endMonthday);
        return row;
    }



    public static Row getNextQuarterStartDay(int num){

        Date    begin           =   DateUtil.endOfQuarter(DateUtil.date());
        Date    enddate         =   DateUtil.parse(DateUtil.endOfMonth(begin).toDateStr()+" 00:00:01", "yyyy-MM-dd HH:mm:ss");
        Date    newMonthday     =   DateUtil.offsetDay(enddate,num-1);
        Date    endMonthday     =   DateUtil.offsetMonth(newMonthday,3);

        Row     row = new Row();
                row.set("startDay",newMonthday);
                row.set("endDay",endMonthday);

        return row;
    }

    public static Row getThisQuarterByCurrentMonth(String  month){

        month  = month+"-01 00:00:01";

        Date    begin           =   DateUtil.beginOfQuarter(DateUtil.parse(month, "yyyy-MM-dd HH:mm:ss"));
        Date    enddate         =   DateUtil.parse(DateUtil.endOfMonth(begin).toDateStr()+" 00:00:01", "yyyy-MM-dd HH:mm:ss");
        Date    newMonthday     =   DateUtil.offsetDay(begin,0);
        Date    endMonthday     =   DateUtil.offsetSecond(DateUtil.offsetMonth(newMonthday,3),-1);

        Row     row = new Row();
        row.set("startDay",newMonthday);
        row.set("endDay",endMonthday);
        return row;
    }

    public static Row getThisQuarterStartDay(int num){

        Date begin = DateUtil.endOfQuarter(DateUtil.date());
        Date    enddate         =   DateUtil.parse(DateUtil.beginOfQuarter(begin).toDateStr()+" 00:00:01", "yyyy-MM-dd HH:mm:ss");
        Date    newMonthday     =   DateUtil.offsetDay(enddate,num-1);
        Date    endMonthday     =   DateUtil.offsetMonth(newMonthday,3);
        Row     row = new Row();
        row.set("startDay",newMonthday);
        row.set("endDay",endMonthday);
        return row;
    }


    public static Row getThisQuarterStartDay(int num,String todaystr){
        Date    todaydate       =   DateUtil.parse(todaystr+" 00:00:01", "yyyy-MM-dd HH:mm:ss");
        Date begin = DateUtil.endOfQuarter(todaydate);
        Date    enddate         =   DateUtil.parse(DateUtil.beginOfQuarter(begin).toDateStr()+" 00:00:01", "yyyy-MM-dd HH:mm:ss");
        Date    newMonthday     =   DateUtil.offsetDay(enddate,num-1);
        Date    endMonthday     =   DateUtil.offsetMonth(newMonthday,3);
        Row     row = new Row();
        row.set("startDay",newMonthday);
        row.set("endDay",endMonthday);
        return row;
    }



    public static Row getNextYearStartDay(int num){
        Date begin = DateUtil.endOfYear(DateUtil.date());
        Date    enddate         =   DateUtil.parse(DateUtil.endOfMonth(begin).toDateStr()+" 00:00:01", "yyyy-MM-dd HH:mm:ss");
        Date    newMonthday     =   DateUtil.offsetDay(enddate,num-1);
        Date    endMonthday     =   DateUtil.offsetMonth(newMonthday,12);
        Row     row = new Row();
        row.set("startDay",newMonthday);
        row.set("endDay",endMonthday);
        return row;
    }

    public static Row getThisYearStartDay(int num){
        Date begin = DateUtil.endOfYear(DateUtil.date());
        Date    enddate         =   DateUtil.parse(DateUtil.beginOfYear(begin).toDateStr()+" 00:00:01", "yyyy-MM-dd HH:mm:ss");
        Date    newMonthday     =   DateUtil.offsetDay(enddate,num-1);
        Date    endMonthday     =   DateUtil.offsetMonth(newMonthday,12);
        Row     row = new Row();
        row.set("startDay",newMonthday);
        row.set("endDay",endMonthday);
        return row;
    }



    public static String getPreMonthFirstDay(){
        return DateUtil.format(DateUtil.lastMonth(), "yyyy-MM-01 00:00:01");
    }

    public static String getCurrentMonthFirstDay(){
        return DateUtil.format(DateUtil.date(), "yyyy-MM-01 00:00:01");
    }

    public static String getMonth(int offset,String format){
        return  DateUtil.format(DateUtil.offsetMonth(DateUtil.date(), -offset),format);
    }


  /* public static void main(String[] args) {

      //  Row  row  =  getThisQuarterByCurrentMonth("2023-08");
        Row  row1  =  getThisWeekStartDay(1,"2023-08-19");
        System.out.println(row1);
        Row  row2  =  getThisWeekStartDay(1,"2023-08-20");
        System.out.println(row2);
       BigDecimal  b = new BigDecimal(0);
       System.out.println( b.compareTo(new BigDecimal(BigInteger.ZERO))==0);
    }
    */



}
